package me.zhengjie.pm.projectcenter.service.impl;

import me.zhengjie.pm.projectcenter.domain.ProjectCenter;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projectcenter.repository.ProjectCenterRepository;
import me.zhengjie.pm.projectcenter.service.ProjectCenterService;
import me.zhengjie.pm.projectcenter.service.dto.ProjectCenterDto;
import me.zhengjie.pm.projectcenter.service.dto.ProjectCenterQueryCriteria;
import me.zhengjie.pm.projectcenter.service.mapper.ProjectCenterMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "projectCenter")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectCenterServiceImpl implements ProjectCenterService {

    private final ProjectCenterRepository projectCenterRepository;

    private final ProjectCenterMapper projectCenterMapper;

    public ProjectCenterServiceImpl(ProjectCenterRepository projectCenterRepository, ProjectCenterMapper projectCenterMapper) {
        this.projectCenterRepository = projectCenterRepository;
        this.projectCenterMapper = projectCenterMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectCenterQueryCriteria criteria, Pageable pageable){
        Page<ProjectCenter> page = projectCenterRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectCenterMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectCenterDto> queryAll(ProjectCenterQueryCriteria criteria){
        return projectCenterMapper.toDto(projectCenterRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectCenterDto findById(Integer id) {
        ProjectCenter projectCenter = projectCenterRepository.findById(id).orElseGet(ProjectCenter::new);
        ValidationUtil.isNull(projectCenter.getId(),"ProjectCenter","id",id);
        return projectCenterMapper.toDto(projectCenter);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectCenterDto create(ProjectCenter resources) {
        return projectCenterMapper.toDto(projectCenterRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectCenter resources) {
        ProjectCenter projectCenter = projectCenterRepository.findById(resources.getId()).orElseGet(ProjectCenter::new);
        ValidationUtil.isNull( projectCenter.getId(),"ProjectCenter","id",resources.getId());
        projectCenter.copy(resources);
        projectCenterRepository.save(projectCenter);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectCenterRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectCenterDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectCenterDto projectCenter : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目", projectCenter.getProjectId());
            map.put("中心", projectCenter.getCenterId());
            map.put("中心承担责任类型", projectCenter.getType());
            map.put("描述", projectCenter.getDescribe());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}